import {
  getOrgAreaList, // 所有运营组织大区
  getServeOrgByLevel, // 所有服务组织大区
  getCurrentUserServeOrgArea, //  获取当前用户服务组织的大区
  getAreaLinkage, // 获取省
  getSimpleCurrentOrgs, // 运营公司
  getAreaTreeModel, // 省市区
  getAreaTree, // 获取所有的省市区
  getServeOrgTree, // 服务组织
  getCompanyListPage // 资产归属
} from '@/api/base'
import cloneDeep from 'lodash/cloneDeep'

// 省市
function filterTreeFn(treeList, flag = true) {
  // flag 删除二级以下的选项
  treeList.forEach(item => {
    if (item.childs.length === 0 || !flag) {
      delete item.childs
    }
    if (item.childs && item.childs.length > 0) {
      filterTreeFn(item.childs, false)
    }
  })
}
// 省市区
function filterTreeFn2(treeList) {
  treeList.forEach(item => {
    if (item.childs.length === 0) {
      delete item.childs
    }
    if (item.childs && item.childs.length > 0) {
      filterTreeFn2(item.childs)
    }
  })
}

const app = {
  state: {
    allOrgAreaArr: [], // 所有运营组织大区， 并非所有服务大区
    allServeOrgByLevelArr: [], // 所有服务大区,并非所有运营大区
    currentUserServeAreaArr: [], // 根据当前用户返回服务大区，并非所有服务大区
    simpleCompanyArr: [], // 资产归属
    provinceArr: [], // 仅仅只有省份
    allProvinceCitys: [], // 所有的省市区
    ServeOrgTree: [], // 服务组织
    SimpleCurrentOrgs: [], // 运营公司
    vehicleUse: [ // 车辆用途
      {
        id: 0,
        name: '租赁提车'
      },
      {
        id: 9,
        name: '以租代售'
      },
      {
        id: 4,
        name: '试用提车'
      },
      {
        id: 5,
        name: '展示提车'
      },
      {
        id: 7,
        name: '备用车提车'
      },
      {
        id: 8,
        name: '自营运力提车'
      },
      {
        id: 11,
        name: '生态圈运力提车'
      }
    ],
    cityTree: [], // 省市级联结构
    areaTree: [], // 省市区级联结构
  },
  mutations: {
    setAllOrgAreaArr(state, data) {
      state.allOrgAreaArr = data
    },
    setAllServeOrgByLevelArr(state, data) {
      state.allServeOrgByLevelArr = data
    },
    setCurrentUserServeOrgArea(state, data) {
      state.currentUserServeAreaArr = data
    },
    setProvinceArr(state, data) {
      state.provinceArr = data
    },
    setSimpleCompanyArr(state, data) {
      state.simpleCompanyArr = data
    },
    setAllProvinceCitys(state, data) {
      state.allProvinceCitys = data
    },
    setServeOrgTree(state, data) {
      state.ServeOrgTree = data
    },
    setSimpleCurrentOrgs(state, data) {
      state.SimpleCurrentOrgs = data
    },
    setCityTree(state, data) {
      state.cityTree = data
    },
    setAreaTree(state, data) {
      state.areaTree = data
    },
  },
  actions: {
    // 获取资产归属
    getSimpleCompanyArr({
      commit,
      state
    }, data = {}) {
      return new Promise(resolve => {
        if (!Array.isArray(state.simpleCompanyArr) || state.simpleCompanyArr.length === 0) {
          getCompanyListPage().then(res => {
            commit('setSimpleCompanyArr', res.data.list)
            resolve(res.data)
          }).catch(() => {
            resolve([])
          })
        } else {
          resolve(state.simpleCompanyArr)
        }
      })
    },

    // 返回所有运营大区,并非所有服务大区(所有服务大区接口：/serveorg/getServeOrgByLevel，下一个接口就是哦)
    getOrgAreaArr({
      commit,
      state
    }, data = {}) {
      return new Promise(resolve => {
        if (!Array.isArray(state.allOrgAreaArr) || state.allOrgAreaArr.length === 0) {
          getOrgAreaList({ level: 1 }).then(res => {
            commit('setAllOrgAreaArr', res.data)
            resolve(res.data)
          }).catch(() => {
            resolve([])
          })
        } else {
          resolve(state.allOrgAreaArr)
        }
      })
    },
    // 返回所有服务大区,并非所有运营大区(所有运营大区接口：/operateCore/org/getOrgAreaList)
    getServeOrgByLevelArr({
      commit,
      state
    }, data = {}) {
      return new Promise(resolve => {
        if (!Array.isArray(state.allServeOrgByLevelArr) || state.allServeOrgByLevelArr.length === 0) {
          getServeOrgByLevel({ level: 1 }).then(res => {
            commit('setAllServeOrgByLevelArr', res.data)
            resolve(res.data)
          }).catch(() => {
            resolve([])
          })
        } else {
          resolve(state.allServeOrgByLevelArr)
        }
      })
    },
    // 根据当前用户返回服务大区，并非所有服务大区(所有服务大区接口：/serveorg/getServeOrgByLevel)
    getCurrentUserServeOrgAreaArr({
      commit,
      state
    }, data = {}) {
      return new Promise(resolve => {
        if (!Array.isArray(state.currentUserServeAreaArr) || state.currentUserServeAreaArr.length === 0) {
          getCurrentUserServeOrgArea().then(res => {
            commit('setCurrentUserServeOrgArea', res.data)
            resolve(res.data)
          }).catch(() => {
            resolve([])
          })
        } else {
          resolve(state.currentUserServeAreaArr)
        }
      })
    },
    // 仅仅用来获取省份
    getProvinceArr({
      commit,
      state
    }) {
      return new Promise(resolve => {
        if (!Array.isArray(state.provinceArr) || state.provinceArr.length === 0) {
          getAreaLinkage({ code: 100000 }).then(res => {
            commit('setProvinceArr', res.data)
            resolve(res.data)
          }).catch(() => {
            resolve([])
          })
        } else {
          resolve(state.provinceArr)
        }
      })
    },
    // 获取所有的省市区
    getAllProvinceCitys({
      commit,
      state
    }) {
      return new Promise(resolve => {
        if (!Array.isArray(state.allProvinceCitys) || state.allProvinceCitys.length === 0) {
          getAreaTree().then(res => {
            commit('setAllProvinceCitys', res.data)
            resolve(res.data)
          }).catch(() => {
            resolve([])
          })
        } else {
          resolve(state.allProvinceCitys)
        }
      })
    },
    // 获取服务组织
    getServeOrgTreeArr({
      commit,
      state
    }) {
      return new Promise(resolve => {
        if (!Array.isArray(state.ServeOrgTree) || state.ServeOrgTree.length === 0) {
          getServeOrgTree().then(res => {
            commit('setServeOrgTree', res.data)
            resolve(res.data)
          }).catch(() => {
            resolve([])
          })
        } else {
          resolve(state.ServeOrgTree)
        }
      })
    },
    // 运营公司
    getSimpleCurrentOrgsArr({
      commit,
      state
    }) {
      return new Promise(resolve => {
        if (!Array.isArray(state.SimpleCurrentOrgs) || state.SimpleCurrentOrgs.length === 0) {
          getSimpleCurrentOrgs().then(res => {
            commit('setSimpleCurrentOrgs', res.data)
            resolve(res.data)
          }).catch(() => {
            resolve([])
          })
        } else {
          resolve(state.SimpleCurrentOrgs)
        }
      })
    },
    // 获取省市级联
    getCityCascader({
      commit,
      state
    }) {
      /*eslint-disable*/
      return new Promise(async(resolve) => {
        if (!Array.isArray(state.cityTree) || state.cityTree.length === 0) {
          let resTree = await getAreaTreeModel();
          let cityTree = cloneDeep(resTree.data)
          let areaTree = cloneDeep(resTree.data)
          filterTreeFn(cityTree);
          filterTreeFn2(areaTree);
          commit('setCityTree', cityTree);
          commit('setAreaTree', areaTree);
          resolve({
            cityTree,
            areaTree,
          });
        } else {
          resolve({
            cityTree: state.cityTree,
            areaTree: state.areaTree,
          })
        }
      })
    }
  }
}

export default app
